#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/13 11:26
# @Author  : 王凯
# @File    : shaanxi_illegal.py
# @Project : spider-man
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import parse_url_params, unquote_url


class BeijingIllegalSpider(BaseTaxIllegalSpider):
    name: str = "beijing_illegal"
    province: str = "北京"
    url: str = "http://beijing.chinatax.gov.cn/bjsat/office/jsp/zdsswfaj/wwquery.jsp"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.beijing.chinatax.gov.cn:8443"})
        url = "http://beijing.chinatax.gov.cn/bjsat/office/jsp/zdsswfaj/wwquery"
        data = {
            "orgCode": "11100000000",
            "bz": "zh",
            "nsrmc": "",
            "nsrsbh": "",
            "zcdz": "",
            "zzjgdm": "",
            "fddbrmc": "",
            "fddbrsfzhm": "",
            "cwfzrmc": "",
            "cwfzrsfzhm": "",
        }
        yield self.FormRequest(url, formdata=data, callback=self.parse)

    def parse(self, response, **kwargs):
        root_url, params = parse_url_params(unquote_url(response.request.body.decode()))
        current_page = kwargs.get("current_page", 1)
        yield from self.parse_list(response, **kwargs)
        page = int(re.findall("结果 ([0-9]{1,8}) 页", response.text)[0])
        if current_page <= page:
            current_page = current_page + 1
            yield self.FormRequest(
                response.url,
                formdata={**params, "dqy": str(current_page)},
                callback=self.parse,
                method="POST",
                cb_kwargs={"current_page": current_page},
            )

    def parse_list(self, response, **kwargs):
        id_list = response.xpath("//input/@onclick")
        root_url, params = parse_url_params(unquote_url(response.request.body.decode()))
        for _id in id_list:
            _id = _id.re_first(r"\('(\d+)'\)")
            if _id:
                data = {
                    "id": f"{_id}",
                    "dq": "null",
                    "ajlx": "null",
                    "ndjd": "null",
                    "bz": "zh",
                    "dqy": params.get("dqy", "1"),
                    "ymdx": "",
                    "nsrmc": params["nsrmc"],
                    "nsrsbh": "",
                    "zcdz": "",
                    "zzjgdm": "",
                    "fddbrmc": "",
                    "fddbrsfzhm": "",
                    "cwfzrmc": "",
                    "cwfzrsfzhm": "",
                    "orgCode": "11100000000",
                }
                yield self.FormRequest(
                    "http://beijing.chinatax.gov.cn/bjsat/office/jsp/zdsswfaj/wwidquery",
                    formdata=data,
                    method="POST",
                    callback=self.parse_detail_info,
                )

    def parse_detail_info(self, response, **kwargs):
        item = NetTaxIllegalProItem()
        info = []
        item.province = self.province
        for i in response.xpath('//table[@width="100%"]//tr'):
            res = [i.xpath("string(.)").get().strip() for i in i.xpath(".//td")]
            if res:
                if len(res) != 2:
                    continue
                info.append(res)

        for k, *v in info:
            v = " ".join(v)
            if "纳税人名称" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.company_name = v
            if "纳税人识别号" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.taxpayer_id = v
            if "组织机构代码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.org_code = v
            if "注册地址" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.address = v
            if "法定代表人或者负责人姓名" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.legal_representative = v
            if "违法期间法人代表或者负责人姓名" in k:
                v = re.sub(r"(<.*?>.*<.*?>|\s*)", "", v, flags=re.S)
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.illegal_legal_representative = v.strip("...")
            if "负有直接责任的财务人员姓名" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_financial = v
            if "实际责任人姓名" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_person = v
            if "负有直接责任的中介机构信息" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split()).replace("null", "")
                item.resp_intermediary = v
            if "案件性质" in k:
                item.illegal_status = v
            if "主要违法事实" in k:
                v = re.sub(r"(<!--.*?-->|\s*|<.*?>)", "", v, flags=re.S)
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                illegal_facts = re.findall(
                    r"(.*(?:主要存在|存在以下问题)(?:[^根]*?[。，;；])*)[^。]*?(?:根据|依照|依据)+", illegal_facts_src
                )
                illegal_facts = illegal_facts[0] if illegal_facts else ""
                if illegal_facts:
                    basis_and_punishment = illegal_facts_src.replace(illegal_facts, "")
                else:
                    illegal_facts = illegal_facts_src
                    basis_and_punishment = ""
                item.basis_and_punishment = basis_and_punishment
                item.illegal_facts = illegal_facts
        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "beijing_illegal"])
